package com.zy.aspects;

import com.zy.controllers.AuthResponse;
import com.zy.web.bean.MyUserDTO;
import lombok.extern.java.Log;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * @program: shopping153_cloud
 * @description:
 * @author: zy
 * @create: 2025-08-30 16:31
 */

@Aspect
@Component
@Log
@Order(  10 )
public class UserLoginAspect {

    @Autowired
    private JmsMessageProducer jmsMessageProducer;

    //切入点表达式
    @Pointcut("execution(* com.zy.service.MyUserService.loginUser(..))")
    public void loginPointcut() {
    }

    //通知的类型  before, after, afterreturning, afterthrowing, around
    @AfterReturning(value="loginPointcut()",returning = "obj")
    public void sendEmailMessageToMQ(   JoinPoint jp ,Object obj ){  // jp是联接点，即原登录方法，通过jp可以获取到登录方法的参数，返回值，异常等信息
        log.info("用户登录成功后，发送一条要发送邮件消息到MQ中保存.。");
        if( obj==null  || !(obj instanceof AuthResponse)){
            return;
        }
        AuthResponse authResponse = (AuthResponse) obj;
        MyUserDTO myUserDTO = authResponse.getMyUserDTO();
        log.info("登录的用户信息:"+myUserDTO.toString());

        jmsMessageProducer.sendMessage(    myUserDTO);


    }


}
